package personnal.jonat.ui;

import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

final class PropertiesTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private final Vector<String> columnNames;
	private Properties configurationProperties;
	private List<String> keys;

	PropertiesTableModel(Properties properties) {
		Vector<String> names = new Vector<String>();
		names.add("Propriété");
		names.add("Valeur");
		this.columnNames = names;
		this.configurationProperties = properties;
		Set<Object> keySet = properties.keySet();
		String[] keys = keySet.toArray(new String[keySet.size()]);
		Arrays.sort(keys);
		this.keys = Arrays.asList(keys);
	}

	public String getColumnName(int col) {
		return columnNames.get(col);
	}

	public Class<String> getColumnClass(int c) {
		return String.class;
	}

	public int getRowCount() {
		return this.configurationProperties.size();
	}

	public int getColumnCount() {
		return columnNames.size();
	}

	public Object getValueAt(int row, int col) {
		if (col == 0) {
			return this.keys.get(row);
		}
		return this.configurationProperties.get(this.keys.get(row));
	}

	public boolean isCellEditable(int row, int col) {
		if (col == 1) {
			return true;
		}
		return false;
	}

	public void setValueAt(Object value, int row, int col) {
		this.configurationProperties.put(getValueAt(row, 0), value);
		fireTableCellUpdated(row, col);
	}
}